<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道列表 -->
    <!-- 
            animated 滑动的动画
            border 底边框线
            swipeable 开启左右手势滑动
      -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated border>
      <!-- 频道 -->
      <van-tab v-for="item in channel" :key="item.id" :title="item.name">
        <articleList :channel="item"></articleList>
      </van-tab>

      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 右侧按钮 -->
      <div slot="nav-right" class="hamburger-btn" @click="isEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->

    <!-- 频道编辑 -->
    <van-popup
      v-model="isEditShow"
      position="bottom"
      close-icon-position="top-left"
      closeable
      :style="{ height: '100%' }"
    >
      <channelEdit
        :my-channel="channel"
        :active-index="active"
        @update-active="active = $event"
        @close="isEditShow = false"
      ></channelEdit>
    </van-popup>
    <!-- 频道编辑 -->
  </div>
</template>

<script>
import { getChannel } from "@/api/channel";
import articleList from "@/views/home/components/article-list";
import channelEdit from "@/views/home/components/channel-edit";
import { getItem } from "@/utiles/storage";
export default {
  // name: "HomeIndex",
  components: {
    articleList,
    channelEdit,
  },
  props: {},
  data() {
    return {
      active: 0,
      channel: [], //定义变量
      isEditShow: false, //频道编辑弹框
    };
  },

  created() {
    // 3 调用
    this.loadChannel();
  },
  methods: {
    // 2 定义方法
    async loadChannel() {
      // 判断是否登录
      if (this.$store.getters.token) {
        // 登录 获取线上用户自己的
        try {
          let { data } = await getChannel();
          console.log(data);
          this.channel = data.data.channels;
        } catch (error) {
          console.log(error);
          return this.$toast.fail("频道获取出错");
        }
      } else {
        // 未登录 判断本地存储是否有
        if (getItem('TOUTIAO_CHANNEL')) {
          // 本地有 用本地的
          this.channel=getItem('TOUTIAO_CHANNEL')
        } else {
          // 本地没有 用线上默认的
          try {
            let { data } = await getChannel();
            console.log(data);
            this.channel = data.data.channels;
          } catch (error) {
            console.log(error);
            return this.$toast.fail("频道获取出错");
          }
        }
      }
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}

/deep/ .channel-tabs {
  .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
    height: 82px;
  }

  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    font-size: 30px;
    color: #777777;
  }

  .van-tab--active {
    color: #333333;
  }

  .van-tabs__nav {
    padding-bottom: 0;
  }

  .van-tabs__line {
    bottom: 8px;
    width: 31px !important;
    height: 6px;
    background-color: #3296fa;
  }

  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  .van-tab__pane {
    font-size: 12px;
  }
}
</style>
